package com.neuqsoft.ymtdemo.utils;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.neuqsoft.ymtdemo.App;
import com.neuqsoft.ymtdemo.R;

import essclib.pingan.ai.request.biap.common.ApiConstants;
import hc.mhis.paic.com.essclibrary.EsscSDK;

/**
 * 切换环境的dialog
 */
public class BottomDialog extends DialogFragment implements View.OnClickListener {

    private RadioGroup rg_en;
    private Button but_ok;
    private int flag = 0;
    public OnDialogListener mlistener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCancelable(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setBackgroundDrawable(null);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        View view = inflater.inflate(R.layout.bottom_dialog, container, false);
        but_ok = view.findViewById(R.id.but_ok);
        rg_en = view.findViewById(R.id.rg_en);
        but_ok.setOnClickListener(this);
        rg_en.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_stanpro:
                        flag = 1;
                        break;
                    case R.id.rb_pro:
                        flag = 2;
                        break;
                    case R.id.rb_dev:
                        flag = 4;
//                    case R.id.rb_cd:
//                        flag = 5;
                        break;
                }
            }
        });
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (flag) {
            case 0:
                Toast.makeText(getActivity(), "请选择接入环境", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                mlistener.onDialogClick("准生产环境");
                EsscSDK.init(App.getApp(), ApiConstants.URL_TEST);
                App.accessKey = "3e3cbf23be4c4c7baf01436f370099f2";
                App.channelNo = "2000000000";
                App.environmentUrl = ApiConstants.URL_TEST;
                this.dismiss();
                break;
            case 2:
                mlistener.onDialogClick("生产环境");
                EsscSDK.init(App.getApp(), ApiConstants.URL_TEST);
                App.accessKey = "71f64ccb634c482180ad7ac891717edb";
                App.channelNo = "1303000001";
                App.environmentUrl = ApiConstants.URL_TEST;
                this.dismiss();
                break;
            case 4:
                mlistener.onDialogClick("开发环境");
                EsscSDK.init(App.getApp(), ApiConstants.URL_DEV);
                // 设置头布局资源信息
                //设置title的颜色，系统状态栏文字明亮昏暗
                EsscSDK.getInstance().setTitleColor("#ffffff", false);
                //头布局文字颜色
                EsscSDK.getInstance().setTextColor("#000000");
                //头布局返回键颜色
                EsscSDK.getInstance().setBackIconColor("#000000");
                //下载类（若自定义菜单内，需要下载服务，可使用此方法，无该需求可忽略）
                EsscSDK.getInstance().setWebViewDownLoadListener(new WebViewDownLoadUtil());
                App.accessKey = "88b925edca3945cb8d6525481058bcd6";
                App.channelNo = "2000000000";
                App.environmentUrl = ApiConstants.URL_DEV;
                this.dismiss();
                break;
//            case 5:
//                mlistener.onDialogClick("承德人社测试环境");
//                EsscSDK.init(App.getApp(), ApiConstants.URL_TEST);
//                App.accessKey = "";
//                App.channelNo = "1308000001";
//                App.environmentUrl = ApiConstants.URL_TEST;
//                this.dismiss();
//                break;

        }

    }

    public interface OnDialogListener {
        void onDialogClick(String str);
    }

    public void setOnDialogListener(OnDialogListener dialogListener) {
        this.mlistener = dialogListener;
    }


}
